package com.wjw.java.games.tankgame3;

/**
 * 发射子弹的线程类：
 */
public class Shot implements Runnable{

    private int x;//子弹的x坐标
    private int y;//子弹的y坐标
    private int direction = 0;//子弹的射击方向：(0上、1右、2下、3左)顺时针
    private int speed = 2;//子弹移动速度,位移几像素
    private boolean isLive = true;//子弹是否存活

    @Override
    public void run() {//射击
        while (true){
            try {
                Thread.sleep(50);//休眠50毫秒,让子弹射击形成轨迹
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //判断子弹的射击方向
            switch (direction){
                case 0://向上射击
                    y -= speed;
                    break;
                case 1://向右射击
                    x += speed;
                    break;
                case 2://向下射击
                    y += speed;
                    break;
                case 3://向左射击
                    x -= speed;
                    break;
            }
            //System.out.println("当前子弹坐标：x="+x+",y="+y);
            //当子弹触及到面板的边界时,销毁,即结束线程
            if(!(x >= 0 && x <= 1000 && y >= 0 && y <= 750)){//如果不在x,y的正常范围内,在MyFrameTankGame中定义了
                System.out.println("关闭：射击敌人坦克线程...");
                isLive = false;//子弹销毁
                break;
            }
        }//while
    }

    //getter、setter、构造器...
    public int getX() {
        return this.x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return this.y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDirection() {
        return this.direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getSpeed() {
        return this.speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public boolean isLive() {
        return this.isLive;
    }

    public void setLive(boolean live) {
        this.isLive = live;
    }

    public Shot(int x, int y, int direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }
}
